package com.css.zfzx.fwsjcj.modules.structure.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @program
 * @description
 * @create 2022/2/14 14:42
 **/
public class BuildingStructureCheck implements IExcelVerifyHandler {
    /**
     * 错误信息
     */
    private StringBuilder msg;
    /**
     * 省市区地区map
     */
    private HashMap<String, List> map;


    public BuildingStructureCheck(HashMap map) {
        this.map = map;
    }

    public BuildingStructureCheck() {
    }

    @Override
    public ExcelVerifyHandlerResult verifyHandler(Object o) {
        // 默认为true
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        msg = new StringBuilder();
        boolean hasMsg = isHasMsg(o);
        if (hasMsg) {
            result.setSuccess(false);
            result.setMsg(msg.toString());
        }
        return result;
    }

    /**
     * 是否有错误消息
     */
    private boolean isHasMsg(Object o) {
        if (isFieldHasMsg(o)) {
            msg.append("填写错误");
            return true;
        }
        return false;
    }

    /**
     * 校验字段是否市下拉列表中的字典值
     */
    private boolean isFieldHasMsg(Object o) {
        boolean flag = false;
        // 所属省份
        String province = getFieldValueByFieldName("belongtoProvince", o);
        if (province != null && !map.containsKey(province)) {
            msg.append("所属省份(填写全名称,检查省市县关系),");
            flag = true;
        } else {
            String city = getFieldValueByFieldName("belongtoCity", o);
            // 所属地级市
            if (!"北京市".equals(province) && !"天津市".equals(province) && !"上海市".equals(province) && !"重庆市".equals(province)) {
                List<String> cities = map.get(province);
                if (city != null && cities != null && !cities.contains(city)) {
                    msg.append("所属地级市(填写全名称,省市关系不匹配),");
                    flag = true;
                } else {
                    // 所属区县
                    String area = getFieldValueByFieldName("belongtoArea", o);
                    List<String> areas = map.get(city);
                    if (area != null && areas != null && !areas.contains(area)) {
                        msg.append("所属区县(填写全名称,市县关系不匹配),");
                        flag = true;
                    }
                }
            }
            // 所属地级市为直辖市
            else {
                if (city != null && !province.equals(city)) {
                    msg.append("所属地级市(填写全名称,直辖市应与省份同名)");
                    flag = true;
                } else {
                    // 所属区县
                    String area = getFieldValueByFieldName("area", o);
                    List<String> areas = map.get(city);
                    if (area != null && areas != null && !areas.contains(area)) {
                        msg.append("所属区县(填写全名称,市县关系不匹配)");
                        flag = true;
                    }
                }
            }
        }

        // 建造年代(下拉选项)
        String buildingAgeField = getFieldValueByFieldName("buildingAge", o);
        Map buildingAge=new HashMap<>();
        buildingAge.put("70s以前","");
        buildingAge.put("70s","");
        buildingAge.put("80s","");
        buildingAge.put("90s","");
        buildingAge.put("00s","");
        buildingAge.put("10s","");
        boolean buildingAgeBoo = buildingAge.containsKey(buildingAgeField);
        if (buildingAgeField != null && !buildingAgeBoo) {
            msg.append("建造年代(下拉选项),");
            flag = true;
        }

        // 场地类型(下拉选项)
        String siteTypeField = getFieldValueByFieldName("siteType", o);
        Map siteType=new HashMap<>();
        siteType.put("I类","");
        siteType.put("II类","");
        siteType.put("III类","");
        siteType.put("IV类","");
        boolean siteTypeBoo = siteType.containsKey(siteTypeField);
        if (siteTypeField != null && !siteTypeBoo) {
            msg.append("场地类型(下拉选项),");
            flag = true;
        }

        // 基础类型(下拉选项)
        String baseTypeField = getFieldValueByFieldName("baseType", o);
        Map baseType=new HashMap<>();
        baseType.put("满堂红","");
        baseType.put("条形基础","");
        baseType.put("砖基础","");
        baseType.put("毛石片石卵石","");
        baseType.put("其他","");
        boolean baseTypeBoo = baseType.containsKey(baseTypeField);
        if (baseTypeField != null && !baseTypeBoo) {
            msg.append("基础类型(下拉选项),");
            flag = true;
        }

        // 墙体类型(下拉选项)
        String wallTypeField = getFieldValueByFieldName("wallType", o);
        Map wallType=new HashMap<>();
        wallType.put("夯土墙","");
        wallType.put("土坯墙","");
        boolean wallTypeBoo = wallType.containsKey(wallTypeField);
        if (wallTypeField != null && !wallTypeBoo) {
            msg.append("墙体类型(下拉选项),");
            flag = true;
        }

        // 墙体砌筑方式(下拉选项)
        String qtdqStyleField = getFieldValueByFieldName("qtdqStyle", o);
        Map qtdqStyle=new HashMap<>();
        qtdqStyle.put("分层交错夯筑","");
        qtdqStyle.put("土坯咬砌","");
        qtdqStyle.put("其他","");
        boolean qtdqStyleBoo = qtdqStyle.containsKey(qtdqStyleField);
        if (qtdqStyleField != null && !qtdqStyleBoo) {
            msg.append("墙体砌筑方式(下拉选项),");
            flag = true;
        }

        // 墙体延性材料(下拉选项)
        String qtyxclField = getFieldValueByFieldName("qtyxcl", o);
        Map qtyxcl=new HashMap<>();
        qtyxcl.put("无","");
        qtyxcl.put("竹筋木条荆条","");
        qtyxcl.put("草筋","");
        boolean qtyxclBoo = qtyxcl.containsKey(qtyxclField);
        if (qtyxclField != null && !qtyxclBoo) {
            msg.append("墙体延性材料(下拉选项),");
            flag = true;
        }

        // 门窗洞口宽度(下拉选项)
        String windowWidthField = getFieldValueByFieldName("windowWidth", o);
        Map windowWidth=new HashMap<>();
        windowWidth.put("大于1.5m","");
        windowWidth.put("小于1.5m","");
        boolean windowWidthBoo = windowWidth.containsKey(windowWidthField);
        if (windowWidthField != null && !windowWidthBoo) {
            msg.append("门窗洞口宽度(下拉选项),");
            flag = true;
        }

        // 承重墙最大间距(下拉选项)
        String czqMaxSpaceField = getFieldValueByFieldName("czqMaxSpace", o);
        Map czqMaxSpace=new HashMap<>();
        czqMaxSpace.put("大于3.2m","");
        czqMaxSpace.put("小于3.2m","");
        boolean czqMaxSpaceBoo = czqMaxSpace.containsKey(czqMaxSpaceField);
        if (czqMaxSpaceField != null && !czqMaxSpaceBoo) {
            msg.append("承重墙最大间距(下拉选项),");
            flag = true;
        }

        // 屋面形式(下拉选项)
        String wmStyleField = getFieldValueByFieldName("wmStyle", o);
        Map wmStyle=new HashMap<>();
        wmStyle.put("平屋面","");
        wmStyle.put("单坡屋面","");
        wmStyle.put("双坡屋面","");
        wmStyle.put("弧面","");
        boolean wmStyleBoo = wmStyle.containsKey(wmStyleField);
        if (wmStyleField != null && !wmStyleBoo) {
            msg.append("屋面形式(下拉选项),");
            flag = true;
        }

        // 屋面材料(下拉选项)
        String wmMaterialField = getFieldValueByFieldName("wmMaterial", o);
        Map wmMaterial=new HashMap<>();
        wmMaterial.put("轻屋面","");
        wmMaterial.put("重屋面","");
        boolean wmMaterialBoo = wmMaterial.containsKey(wmMaterialField);
        if (wmMaterialField != null && !wmMaterialBoo) {
            msg.append("屋面材料(下拉选项),");
            flag = true;
        }

        // 硬山搁檩(下拉选项)
        String ysgeField = getFieldValueByFieldName("ysge", o);
        Map ysge=new HashMap<>();
        ysge.put("0（否）","");
        ysge.put("1（是）","");
        boolean ysgeBoo = ysge.containsKey(ysgeField);
        if (ysgeField != null && !ysgeBoo) {
            msg.append("硬山搁檩(下拉选项),");
            flag = true;
        }

        // 屋、楼盖拉结措施(下拉选项)
        String wlgljcsField = getFieldValueByFieldName("wlgljcs", o);
        Map wlgljcs=new HashMap<>();
        wlgljcs.put("无","");
        wlgljcs.put("山墙和隔墙与屋架拉结","");
        boolean wlgljcsBoo = wlgljcs.containsKey(wlgljcsField);
        if (wlgljcsField != null && !wlgljcsBoo) {
            msg.append("屋、楼盖拉结措施(下拉选项),");
            flag = true;
        }

        // 出屋面构件(下拉选项)
        String cwmgjField = getFieldValueByFieldName("cwmgj", o);
        Map cwmgj=new HashMap<>();
        cwmgj.put("0（无）","");
        cwmgj.put("1（女儿墙）","");
        cwmgj.put("2（烟囱）","");
        cwmgj.put("3（无构造柱楼梯间）","");
        cwmgj.put("4（其他）","");
        boolean cwmgjBoo = cwmgj.containsKey(cwmgjField);
        if (cwmgjField != null && !cwmgjBoo) {
            msg.append("出屋面构件(下拉选项),");
            flag = true;
        }


        // 墙体圈梁(下拉选项)
        String wallRingBeamField = getFieldValueByFieldName("wallRingBeam", o);
        Map wallRingBeam=new HashMap<>();
        wallRingBeam.put("有圈梁","");
        wallRingBeam.put("无圈梁","");
        boolean wallRingBeamBoo = wallRingBeam.containsKey(wallRingBeamField);
        if (wallRingBeamField != null && !wallRingBeamBoo) {
            msg.append("墙体圈梁(下拉选项),");
            flag = true;
        }

        // 建筑现状(下拉选项)
        String buildingSituationField = getFieldValueByFieldName("buildingSituation", o);
        Map buildingSituation=new HashMap<>();
        buildingSituation.put("完好","");
        buildingSituation.put("墙体歪闪","");
        buildingSituation.put("墙体开裂","");
        buildingSituation.put("墙体腐蚀酥碎","");
        buildingSituation.put("楼屋盖构件有变形","");
        buildingSituation.put("地基不均匀沉降","");
        boolean buildingSituationBoo = buildingSituation.containsKey(buildingSituationField);
        if (buildingSituationField != null && !buildingSituationBoo) {
            msg.append("建筑现状(下拉选项),");
            flag = true;
        }

        //经度
        String longitudeField = getFieldValueByFieldName("longitude", o);
        if(longitudeField!=null && !"".equals(longitudeField)){
            double longitude=Double.valueOf(longitudeField);
            if(longitude>180.0 || longitude<-180.0){
                msg.append("经度,");
                flag = true;
            }
        }
        //纬度
        String latitudeField = getFieldValueByFieldName("latitude", o);
        if(!"".equals(latitudeField) && latitudeField!=null) {
            double latitude = Double.valueOf(latitudeField);
            if (latitude > 90.0 || latitude < -90.0) {
                msg.append("纬度,");
                flag = true;
            }
        }

        return flag;
    }





    /**
     * 根据属性名获取属性值
     *
     * @param fieldName 属性名
     * @param object    对象
     * @return
     */
    private String getFieldValueByFieldName(String fieldName, Object object) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = object.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(object, new Object[]{});
            return value.toString();
        } catch (Exception e) {
            return null;
        }
    }
}
